﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DomainModel.Domain;
using DomainModel.Interfaces;

namespace DomainModel.Repositories
{
    public class RefereeRepository : IRefereeRepository
    {
        private readonly SqlConnection _con;

        public RefereeRepository(string connectionString)
        {
            _con = new SqlConnection(connectionString);
        }

        public List<IReferee> GetPossibleReferees(IMatch match)
        {
            using (var con = _con)
            {
                var refList = new List<IReferee>();
                con.Open();
                const string cmdTxt = @"SELECT Referees.RefereeId, navn, dommernr, Absence.[Date], [DayName]
		                                FROM Referees LEFT JOIN Absence
		                                ON Referees.RefereeId = Absence.RefereeId
		                                LEFT JOIN DayRefMapping
		                                ON Referees.RefereeId = DayRefMapping.RefereeId
		                                LEFT JOIN [Days]
		                                ON Days.DayId = DayRefMapping.DayId
		                                WHERE plac = '@rank'
		                                AND (Absence.[Date] <> '@matchday' OR Absence.[date] is null)
		                                AND [Days].[DayName] = DATENAME( weekday , '@matchday' )";
                var cmd = new SqlCommand(cmdTxt, con);
                cmd.Parameters.AddWithValue("rank", match.MatchLevelId);
                cmd.Parameters.AddWithValue("matchday", match.MatchDay);

                var resultSet = cmd.ExecuteReader();
                while (resultSet.Read())
                {
                    refList.Add(new Referee(resultSet["FirstName"].ToString(), resultSet["LastName"].ToString(), (int)resultSet["dommernr"]) { RefereeId = (int)resultSet["RefereeId"] });
                }
                return refList;
            }
        }

        //TODO: CONSIDER!! This or just updateMatch? #WhereToPutLogic
        public bool AddRefereeToMatch(IReferee referee, IMatch match)
        {
            using (var con = _con)
            {
                try
                {
                    con.Open();
                    const string cmdTxt = @"UPDATE Matches SET RefereeID = @refereeId WHERE MatchId = @matchId";
                    var cmd = new SqlCommand(cmdTxt, con);
                    cmd.Parameters.AddWithValue("refereeId", referee.RefereeId);
                    cmd.Parameters.AddWithValue("matchId", match.MatchId);
                    return true;
                }
                catch (Exception)
                {
                    //TODO: LOG HERE!!!
                    return false;
                }
            }
        }

        public void AddMatch(IMatch match)
        {
            throw new NotImplementedException();
        }

        public void UpdateMatch(IMatch match)
        {
            throw new NotImplementedException();
        }

        public void DeleteMatch(IMatch match)
        {
            throw new NotImplementedException();
        }

        public void AddReferee(IReferee referee)
        {
            throw new NotImplementedException();
        }

        public void UpdateReferee(IReferee referee)
        {
            throw new NotImplementedException();
        }

        public void DeleteReferee(IReferee referee)
        {
            throw new NotImplementedException();
        }
    }
}
